﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace Tilver.Data
{
    /// <summary>
    /// 数据库访问接口
    /// </summary>
    public interface IDbAdo
    {
        /// <summary>
        /// 数据库访问对象
        /// </summary>
        DbDataBase DataBase { get; }

        /// <summary>
        /// DbCommand对象
        /// </summary>
        CommandWrapper Command { get; }

        /// <summary>
        /// 执行查询语句并返回首行首列数据
        /// </summary>
        /// <param name="tm">数据库事务管理对象</param>
        /// <returns></returns>
        object ExecuteScalar(TransactionManager tm = null);

        /// <summary>
        /// 执行SQL语句并返回受影响的行数
        /// </summary>
        /// <param name="tm">数据库事务管理对象</param>
        /// <returns></returns>
        int ExecuteNonQuery(TransactionManager tm = null);

        /// <summary>
        /// 执行SQL语句并返回唯一实体对象(T)
        /// </summary>
        /// <typeparam name="T">IDataReader对象映射的实体对象类型</typeparam>
        /// <param name="rowMapper">IDataReader对象到实体对象的映射方法，如果为null，则默认使用DataColumnAttribute属性定义的元数据进行反射获取实体对象</param>
        /// <returns></returns>
        T ExecuteSingle<T>(Func<IDataReader, T> rowMapper = null, TransactionManager tm = null);

        /// <summary>
        /// 执行SQL语句并返回结果集
        /// </summary>
        /// <param name="tm">数据库事务管理对象</param>
        /// <returns></returns>
        DbDataReader ExecuteReader(TransactionManager tm = null);

        /// <summary>
        /// 执行SQL语句并返回实体对象集合
        /// </summary>
        /// <typeparam name="T">IDataReader对象映射的实体对象类型</typeparam>
        /// <param name="rowMapper">IDataReader对象到实体对象的映射方法，如果为null，则默认使用ColumnMapperAttribute定义的元数据进行反射获取实体对象</param>
        /// <param name="tm"></param>
        /// <returns></returns>
        List<T> ExecuteList<T>(Func<IDataReader, T> rowMapper = null, TransactionManager tm = null);

        /// <summary>
        /// 执行SQL语句并返回实体对象集合
        /// </summary>
        /// <param name="T"></param>
        /// <param name="rowMapper"></param>
        /// <param name="tm"></param>
        /// <returns></returns>
        IEnumerable<T> ExecuteMutil<T>(Func<IDataReader, T> rowMapper = null, TransactionManager tm = null);

        /// <summary>
        /// 执行SQL语句并返回DataTable对象数据
        /// </summary>
        /// <param name="tm"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(TransactionManager tm = null);

        /// <summary>
        /// 创建DbParameter对象
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="dbType">参数的 DbType</param>
        /// <param name="size">列中数据的最大大小（以字节为单位）</param>
        /// <param name="direction">指示参数是只可输入、只可输出、双向还是存储过程返回值参数</param>
        /// <param name="value">参数的值</param>
        /// <returns></returns>
        //DbParameter CreateParameter(string name, object value, DbType dbType = DbType.String, int size = 0, ParameterDirection direction = ParameterDirection.Input);
    }
}
